<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-01-20
 * Time: 16:23
 */

namespace app\admin\controller;


use app\admin\model\HospitalModel;
use think\Session;

class Hospital extends Base
{
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where['user_name'] = ['like', '%' . $param['searchText'] . '%'];
            }
            $user = new HospitalModel();
            $selectResult = $user->gethospitalByWhere($where, $offset, $limit);

            $status = config('hospital_status');
            $tuijian = config('hospital_tuijian');
            $renzheng = config('hospital_renzheng');

            // 拼装参数
            foreach($selectResult as $key=>$vo){

                $selectResult[$key]['status'] = $status[$vo['status']];
                $selectResult[$key]['tuijian'] = $tuijian[$vo['tuijian']];
                $selectResult[$key]['renzheng'] = $renzheng[$vo['renzheng']];

                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }

            $return['total'] = $user->getAllHospital($where);  //总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }


    public function hospitaladd()
    {
        $user_name = Session::get('username');
        if(request()->isPost()){

            $param = input('post.');

//            var_dump($param);exit;
            $param['hospital_tcbl'] = $param['hospital_tcbl'].'%';
            $param['hospital_people'] = $user_name;
            $param['status'] = 1;
            $param['hospital_time'] = date("Y-m-d h:i:sa");

            $user = new HospitalModel();
            $flag = $user->insertHospital($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
//        var_dump(config('hospital_tuijian'));exit;
        $this->assign([
            'tuijian' => config('hospital_tuijian'),
            'renzheng' => config('hospital_renzheng')
        ]);
        return $this->fetch();
    }









    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'hospital/hospitaledit',
                'href' => url('hospital/hospitalEdit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'hospital/hospitaldel',
                'href' => "javascript:hospitalDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }

}